﻿package com.kuanter.kuanterauto.activity;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;

import com.kuanter.kuanterauto.R;
import com.kuanter.kuanterauto.cache.ObjectCache;
import com.kuanter.kuanterauto.constants.HttpConfigAPI;
import com.kuanter.kuanterauto.constants.MainConstants;
import com.kuanter.kuanterauto.entity.ConsumerLog;
import com.kuanter.kuanterauto.net.HttpTask;
import com.kuanter.kuanterauto.utils.DataLoader;
import com.kuanter.kuanterauto.utils.SharePrefenceUtil;
import com.kuanter.kuanterauto.utils.StringUtil;
import com.tencent.mm.sdk.platformtools.Log;
import com.umeng.analytics.MobclickAgent;

public class MyConsumerDetailActivity extends WuyangBaseActivity implements  OnRatingBarChangeListener{
	private TextView my_consumer_detail_include_2_tv_service_kind_title;
	private TextView my_consumer_detail_include_2_tv_outOfPockte_money_title;
	private TextView my_consumer_detail_include_2_tv_trade_status_title;
	private TextView my_consumer_detail_include_2_tv_trade_time_title;
	private int consumerLogIndex ;
	private ConsumerLog consumerLog ;
	private List<ConsumerLog> consumerLogList ;
	private TextView my_consumer_detail_include_2_tv_website_name_data;
	private TextView my_consumer_detail_include_2_tv_service_kind_data;
	private TextView my_consumer_detail_include_2_tv_outOfPockte_money_data;
	private TextView my_consumer_detail_include_2_tv_trade_status_data;
	private TextView my_consumer_detail_include_2_tv_trade_time_data;
	
	private float rating;
	/*private ImageView include_1tv_star_1tv_Star1;
	private ImageView include_1tv_star_1tv_Star2;
	private ImageView include_1tv_star_1tv_Star3;
	private ImageView include_1tv_star_1tv_Star4;
	private ImageView include_1tv_star_1tv_Star5;*/
	
	/*private boolean star1_flag = false;
	private boolean star2_flag = false;
	private boolean star3_flag = false;
	private boolean star4_flag = false;
	private boolean star5_flag = false;*/
	private Button back_btn;
//	private int starNum = 0;
	private TextView tvSiteCredits;
	private ImageView score_iv;
	private DataLoader dataLoader;
	private int userId;
	
	Handler handler = new Handler() {

		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case HttpTask.RESULT_OK:
				finishProgress();
				// String result = msg.getData().getString("result");
				String result = (String) msg.obj;
				Log.i("wuyang", result);
				if (!result.equals("网络连接错误")) {
					try {
						if (StringUtil.isSuccess(result)) {// 判断返回数据中 status
							showToast("打分成功");							// 字段值是否为false
//								ratingBar.setClickable(false);
								ratingBar.setRating(rating);
								ratingBar.setEnabled(false);
							finishProgress();
						} else {
							finishProgress();
							// 显示返回数据中 errorMsg 字段值
							showToast(StringUtil.geterrorMsg(result));
						}
					} catch (JSONException e) {
						finishProgress();
						showToast("登录失败");
						e.printStackTrace();
					}
				} else {
					finishProgress();
					showToast("登录失败,网络错误!");
				}
				break;
			case HttpTask.RESULT_ERROR:
				finishProgress();
				showToast("网络错误!");
				break;
			}
		}
	};
	private RatingBar ratingBar;
	
	
	public void onResume() {
	    super.onResume();
	    MobclickAgent.onResume(this);
	}
	public void onPause() {
	    super.onPause();
	    MobclickAgent.onPause(this);
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		setContentView(R.layout.my_consumer_detail);
		super.onCreate(savedInstanceState);
		ratingBar = (RatingBar)this.findViewById(R.id.ratingBar);
		ratingBar.setOnRatingBarChangeListener(this);
		userId = SharePrefenceUtil.getUserID(this);
		consumerLogIndex = getIntent().getIntExtra("consumerLogIndex", 0);
		consumerLogList = (List<ConsumerLog>)ObjectCache.get("consumerList");
		consumerLog = consumerLogList.get(consumerLogIndex);
		this.setHeaderControlViewText();
		this.initBodyControlView();
		this.setBodyControlViewText();
		this.registerListener();
		if(!"".equals(consumerLog.getVoteState())){
			if("2".equals(consumerLog.getVoteState())||"1".equals(consumerLog.getState())||"9".equals(consumerLog.getState())){
//			showToast("您上次评过分了哦~");
			ratingBar.setClickable(false);
				ratingBar.setEnabled(false);
			}else if(("1".equals(consumerLog.getVoteState()) && "4".equals(consumerLog.getState()))){
				ratingBar.setEnabled(true);
			}
		}	
	}

	@Override
	protected void setHeaderControlViewText() {
		// TODO Auto-generated method stub
		super.setHeaderTitleText(super.headerTitle, "消费详情");
		super.setHeaderLeftText(super.headerLeft, "返回");
		super.setHeaderRightText(super.headerRight, null);
	}

	@Override
	protected void initBodyControlView() {
		// TODO Auto-generated method stub
		View my_consumer_detail_include_2_tv_website_name_item = this.findViewById(R.id.my_consumer_detail_include_2_tv_website_name);
		//数据待定 
		my_consumer_detail_include_2_tv_website_name_data = (TextView) my_consumer_detail_include_2_tv_website_name_item.findViewById(R.id.include_2_tv_TVdata);
		View my_consumer_detail_include_2_tv_service_kind_item = this.findViewById(R.id.my_consumer_detail_include_2_tv_service_kind);
		my_consumer_detail_include_2_tv_service_kind_title =(TextView) my_consumer_detail_include_2_tv_service_kind_item.findViewById(R.id.include_2_tv_TVtitle);
		//数据待定 
		my_consumer_detail_include_2_tv_service_kind_data = (TextView) my_consumer_detail_include_2_tv_service_kind_item.findViewById(R.id.include_2_tv_TVdata);
		my_consumer_detail_include_2_tv_service_kind_data.setTextColor(this.getResources().getColor(R.color.blue));
		View my_consumer_detail_include_2_tv_outOfPockte_money_item =  this.findViewById(R.id.my_consumer_detail_include_2_tv_outOfPockte_money);
		my_consumer_detail_include_2_tv_outOfPockte_money_title = (TextView) my_consumer_detail_include_2_tv_outOfPockte_money_item.findViewById(R.id.include_2_tv_TVtitle);
		//数据待定
		my_consumer_detail_include_2_tv_outOfPockte_money_data = (TextView) my_consumer_detail_include_2_tv_outOfPockte_money_item.findViewById(R.id.include_2_tv_TVdata);
		View my_consumer_detail_include_2_tv_trade_status_item =  this.findViewById(R.id.my_consumer_detail_include_2_tv_trade_status);
		my_consumer_detail_include_2_tv_trade_status_title = (TextView) my_consumer_detail_include_2_tv_trade_status_item.findViewById(R.id.include_2_tv_TVtitle);
		//数据待定
		my_consumer_detail_include_2_tv_trade_status_data = (TextView) my_consumer_detail_include_2_tv_trade_status_item.findViewById(R.id.include_2_tv_TVdata);
		View my_consumer_detail_include_2_tv_trade_time_item = this.findViewById(R.id.my_consumer_detail_include_2_tv_trade_time);
		my_consumer_detail_include_2_tv_trade_time_title = (TextView) my_consumer_detail_include_2_tv_trade_time_item.findViewById(R.id.include_2_tv_TVtitle);
		//数据待定
		my_consumer_detail_include_2_tv_trade_time_data = (TextView) my_consumer_detail_include_2_tv_trade_time_item.findViewById(R.id.include_2_tv_TVdata);
	
		//网点打分
		/*
		include_1tv_star_1tv_Star1 = (ImageView) this.findViewById(R.id.include_1tv_star_1tv_Star1);
		include_1tv_star_1tv_Star2 = (ImageView) this.findViewById(R.id.include_1tv_star_1tv_Star2);
		include_1tv_star_1tv_Star3 = (ImageView) this.findViewById(R.id.include_1tv_star_1tv_Star3);
		include_1tv_star_1tv_Star4 = (ImageView) this.findViewById(R.id.include_1tv_star_1tv_Star4);
		include_1tv_star_1tv_Star5 = (ImageView) this.findViewById(R.id.include_1tv_star_1tv_Star5);*/
		//返回，提交
		back_btn = (Button)this.findViewById(R.id.header_left);
	}

	@Override
	protected void setBodyControlViewText() {
		// TODO Auto-generated method stub
		my_consumer_detail_include_2_tv_service_kind_title.setText("服务项目：");
		my_consumer_detail_include_2_tv_outOfPockte_money_title.setText("积分消耗：");
		my_consumer_detail_include_2_tv_trade_status_title.setText("交易状态：");
		my_consumer_detail_include_2_tv_trade_time_title.setText("交易时间：");
		//设置数据
		my_consumer_detail_include_2_tv_website_name_data.setText(consumerLog.getConsumptionShop());
		my_consumer_detail_include_2_tv_service_kind_data.setText(consumerLog.getConsumptionType());
		my_consumer_detail_include_2_tv_outOfPockte_money_data.setText(consumerLog.getConsumptionAmount());
		if(null == consumerLog.getState()){
			my_consumer_detail_include_2_tv_trade_status_data.setText("已完成");
		}else {
			switch (Integer.valueOf(consumerLog.getState())) {
			case 1:
				my_consumer_detail_include_2_tv_trade_status_data.setText("处理中");
				break;
			case 4:
				my_consumer_detail_include_2_tv_trade_status_data.setText("已完成");
				break;
			case 9:
				my_consumer_detail_include_2_tv_trade_status_data.setText("已取消");
				break;
			}
		}
		my_consumer_detail_include_2_tv_trade_time_data.setText(consumerLog.getConsumptionTime());
		score_iv = (ImageView)this.findViewById(R.id.score_iv);
		if(null == consumerLog.getScore()||"".equals(consumerLog.getScore())){
			score_iv.setImageResource(R.drawable.point0);
		}else {
			int flag = (int) Math.round(Double.valueOf(consumerLog.getScore()));
			switch (flag) {
			case 0:
				ratingBar.setRating(0);
				score_iv.setImageResource(R.drawable.point0);
				break;
			case 1:
				score_iv.setImageResource(R.drawable.point10);
				ratingBar.setRating(1);
				break;
			case 2:
				score_iv.setImageResource(R.drawable.point20);
				ratingBar.setRating(2);
				break;
			case 3:
				score_iv.setImageResource(R.drawable.point30);
				ratingBar.setRating(3);
				break;
			case 4:
				score_iv.setImageResource(R.drawable.point40);
				ratingBar.setRating(4);
				break;
			case 5:
				score_iv.setImageResource(R.drawable.point50);
				ratingBar.setRating(5);
				break;
			}
		}
	}

	@Override
	protected void registerListener() {
		// TODO Auto-generated method stub
		/*include_1tv_star_1tv_Star1.setOnClickListener(this);
		include_1tv_star_1tv_Star2.setOnClickListener(this);
		include_1tv_star_1tv_Star3.setOnClickListener(this);
		include_1tv_star_1tv_Star4.setOnClickListener(this);
		include_1tv_star_1tv_Star5.setOnClickListener(this);*/
		back_btn.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		/*case R.id.include_1tv_star_1tv_Star1:
			if(star1_flag == false){
				include_1tv_star_1tv_Star1.setImageResource(R.drawable.btn_star_b_normal);
				star1_flag = true;
				starNum++;
			}else{
				include_1tv_star_1tv_Star1.setImageResource(R.drawable.btn_star_b_pressed);
				star1_flag = false;
				starNum--;
			}
			break;
		case R.id.include_1tv_star_1tv_Star2:
			if(star2_flag == false){
				include_1tv_star_1tv_Star2.setImageResource(R.drawable.btn_star_b_normal);
				star2_flag = true;
				starNum++;
			}else{
				include_1tv_star_1tv_Star2.setImageResource(R.drawable.btn_star_b_pressed);
				star2_flag = false;
				starNum--;
			}
			break;
		case R.id.include_1tv_star_1tv_Star3:
			if(star3_flag == false){
				include_1tv_star_1tv_Star3.setImageResource(R.drawable.btn_star_b_normal);
				star3_flag = true;
				starNum++;
			}else{
				include_1tv_star_1tv_Star3.setImageResource(R.drawable.btn_star_b_pressed);
				star3_flag = false;
				starNum--;
			}
			break;
		case R.id.include_1tv_star_1tv_Star4:
			if(star4_flag == false){
				include_1tv_star_1tv_Star4.setImageResource(R.drawable.btn_star_b_normal);
				star4_flag = true;
				starNum++;
			}else{
				include_1tv_star_1tv_Star4.setImageResource(R.drawable.btn_star_b_pressed);
				star4_flag = false;
				starNum--;
			}
			break;
		case R.id.include_1tv_star_1tv_Star5:
			if(star5_flag == false){
				include_1tv_star_1tv_Star5.setImageResource(R.drawable.btn_star_b_normal);
				star5_flag = true;
				starNum++;
			}else{
				include_1tv_star_1tv_Star5.setImageResource(R.drawable.btn_star_b_pressed);
				star5_flag = false;
				starNum--;
			}
			break;*/
		case R.id.header_left:
			if(rating > 0){
				if(null == consumerLog.getVoteState()||"".equals(consumerLog.getVoteState())){
					//showToast("您未给该网店投票打分哦~");
					dataLoader = new DataLoader(this);
					List<NameValuePair> params = new ArrayList<NameValuePair>();
					params.add(new BasicNameValuePair("userId", userId+""));
					params.add(new BasicNameValuePair("id",consumerLog.getId()));
					params.add(new BasicNameValuePair("score", (int)rating+""));
					dataLoader.DisplayData(MainConstants.HOST_test + HttpConfigAPI.GRADE,
							params, handler, "", false);
					showProgress("正在加载数据...");
				}else {
					if(!"".equals(consumerLog.getVoteState())){
						switch (Integer.valueOf(consumerLog.getVoteState())) {
						case 1://未投票
							//showToast("您未给该网店投票打分哦~");
							dataLoader = new DataLoader(this);
							List<NameValuePair> params = new ArrayList<NameValuePair>();
							params.add(new BasicNameValuePair("userId", userId+""));
							params.add(new BasicNameValuePair("id",consumerLog.getId()));
							params.add(new BasicNameValuePair("score", (int)rating+""));
							dataLoader.DisplayData(MainConstants.HOST_test + HttpConfigAPI.GRADE,
									params, handler, "", false);
							showProgress("正在打分中...");
							break;
						case 2://已投票
//					showToast("您上次评过分了哦~");
//						ratingBar.setRating(rating);
							ratingBar.setIsIndicator(false);
							break;
						}
					}
				}
			}
			this.finish();
			break;
		}
	}

	@Override
	protected void getControlViewText() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onRatingChanged(RatingBar ratingBar, float rating,
			boolean fromUser) {
		// TODO Auto-generated method stub
//		Log.i("wu", rating+"");
		this.rating = rating;
	}
}
